﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WoBian.Models.Admin.Login;
using WoBian.Controllers.Common;
using WoBian.Lib.Enum;
using WoBian.ServiceModel;
using WoBian.Lib.ConstString;

namespace WoBian.Controllers.Admin
{
    public class AdminLoginController : BaseController
    {
        //
        // GET: /AdminLogin/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(AdminLoginModel aModel)
        {
            try
            {
                var aInfo = aModel.Login();
                if (aInfo.LoginStatus == LoginStatusEnum.Successed)
                {
                    MyApplicationContext.Current.UserId = aInfo.UserInfo.UserId;
                    MyApplicationContext.Current.NickName = aInfo.UserInfo.NickName;
                    MyApplicationContext.Current.Name = aInfo.UserInfo.Name;
                    //获取权限方法待实现
                    //MyApplicationContext.Current.Roles = null;
                    return RedirectToRoute(MapRouteControllers.AdminIndex);
                }
                else
                {
                    return View(aModel);
                }
            }
            catch(Exception ex)
            {
                return ShowErrorMessage(ex.Message);
            }
        }

        public ActionResult ValidateCode()
        {
            ValidateCodeModel aModel = new ValidateCodeModel();
            aModel.Image = aModel.CreateValidateCodeImage();
            return File(aModel.Image,@"image/gif");
        }
    }
}
